﻿using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using VersionComparer.Views;

namespace VersionComparer.DataModel.Actions
{
    [ActionAttribute("Test connection string", 20, System.Windows.Forms.Keys.Control | System.Windows.Forms.Keys.Enter)]
    public class TestDatabaseConnectionAction : SpecificAction
    {

        public override bool MayBeExecuted(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            if (focusedSelectedItems.Length != 1)
                return false;
            DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
            foreach (ComparableItem item in focusedSelectedItems)
            {
                try
                {
                    if (item.Text.Contains(';'))
                    {
                        builder.ConnectionString = item.Text;
                        return true;
                    }
                }
                catch
                {
                }
            }
            return false;
        }

        protected override void ExecuteAction(MainForm mainForm, ComparableItem[] focusedSelectedItems, ComparableItem[] unfocusedSelectedItems)
        {
            DbConnectionStringBuilder builder = new DbConnectionStringBuilder();
            foreach (ComparableItem item in focusedSelectedItems)
            {
                try
                {
                    builder.ConnectionString = item.Text;
                    ConnectionStringTest testForm = new ConnectionStringTest();
                    testForm.ConnectionString = item.Text;
                    testForm.Show();
                    
                }
                catch
                {
                }
            }
        }
    }
}
